package com.example.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.admin.Dto.HuaweiAuthDto;
import com.example.admin.base.ResponseResult;
import com.example.admin.bean.Device;
import com.example.admin.service.Lotservice;
import com.example.admin.utils.StringUtils;
import com.example.admin.utils.UrlUtil;
import org.codehaus.groovy.runtime.StringGroovyMethods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/iot")
public class LotWeb {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    Lotservice lotservice;
    public String auth(){
        HuaweiAuthDto huaweiAuthDto = new HuaweiAuthDto();
        ResponseEntity<String> response = restTemplate.postForEntity("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens", huaweiAuthDto, String.class);
        List<String> strings = response.getHeaders().get("X-Subject-Token");
        String token = strings.get(0);
        redisTemplate.opsForValue().set("huaweiyun:auth:token",token,23, TimeUnit.HOURS);
        return token;
    }
    @GetMapping("/queryproductlist")
    public ResponseResult list(String lastid,@RequestParam(defaultValue = "5") Integer limit){
        String token = redisTemplate.opsForValue().get("huaweiyun:auth:token");
        if (StringUtils.isEmpty(token)){
           token =  auth();
        }
        StringBuffer url = new StringBuffer("https://6cb358bfc7.st1.iotda-app.cn-north-4.myhuaweicloud.com:443/v5/iot/574b551ae0064ae5a120a46b253d46e4/products");
        Boolean hasnext = false;
        if (StringUtils.isNotEmpty(lastid)){
            url.append("?last_id=").append(lastid);
            hasnext = true;
        }
        if (hasnext == true){
            url.append("&limit=").append(limit);
        }else {
            url.append("?limit=").append(limit);
        }
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("X-Auth-Token",token);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Object> entity = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, String.class);
        String body = response.getBody();
        Object parsebody = JSONObject.parse(body);
        return ResponseResult.success(parsebody);
    }
    @PostMapping("/querydevice")
    public ResponseResult querydevice(@RequestParam Map<String,Object> params){
        String token = redisTemplate.opsForValue().get("huaweiyun:auth:token");
        if (StringUtils.isEmpty(token)){
            token =  auth();
        }
        StringBuffer url = new StringBuffer("https://6cb358bfc7.st1.iotda-app.cn-north-4.myhuaweicloud.com:443/v5/iot/574b551ae0064ae5a120a46b253d46e4/devices");
        String geturl = UrlUtil.geturl(params);
        url.append(geturl);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("X-Auth-Token",token);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Object> objectHttpEntity = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> resp = restTemplate.exchange(url.toString(), HttpMethod.GET, objectHttpEntity, String.class);
        String body = resp.getBody();
        System.out.println(body);
        System.out.println(url);
        return ResponseResult.success(JSONObject.parse(body));
    }
    @PostMapping("/adddevice")
    public ResponseResult adddevice(@RequestBody Device device){
        lotservice.adddevice(device);
        return ResponseResult.success();
    }
    @PostMapping("/searchdevice")
    public ResponseResult searchdevice(String device_id){
        Object result = lotservice.searchdevice(device_id);
        return ResponseResult.success(result);
    }
    @PostMapping("/whatchobjectmodel")
    public ResponseResult whatchobjectmodel(String product_id){
        Object result = lotservice.whatchobjectmodel(product_id);
        return ResponseResult.success(result);
    }
    @PostMapping("/fixdevice")
    public ResponseResult fixdevice(@RequestBody Device device){
        lotservice.fixdevice(device);
        return ResponseResult.success();
    }
    @PostMapping("/deletedevice")
    public ResponseResult deletedevice(String device_id){
        try {
            lotservice.deletedevice(device_id);
        } catch (Exception e) {
            return ResponseResult.error(e.getMessage());
        }
        return ResponseResult.success();

    }
}
